package com.xinlu.derek.common;

import org.apache.commons.lang.StringUtils;

import com.xinlu.common.exception.CommonException;
import com.xinlu.common.log.Logger;
import com.xinlu.util.tool.ReflectUtil;

public class BaseActionUtil {
	
	protected static Logger logger = Logger.getLogger(BaseActionUtil.class);

	/**
	 * @see com.sinyee.common.BaseAction
	 */
	protected BaseAction action;

	public BaseActionUtil(BaseAction action) {

		this.action = action;
	}
	
	/**
	 * 
	 * function: 判断是否当前页面的访问，比如IndexAction 访问则默认该action的页面为index
	 * 
	 * @param pageId
	 *            form过来的页面id 比如 index
	 * @return true为是当前页面，false为其他页面的访问
	 * 
	 * @author Rain Stars Mar 7, 2009 3:47:25 PM
	 */
	public boolean isCurrentPage(String pageId) {

		boolean flag = false;

		if (StringUtils.isNotEmpty(pageId)) {

			// 得到action类名
			String actionClassName = ReflectUtil.getClassBefine(action.getClass().getName());

			if (actionClassName != null) {

				String actionPageId = actionClassName.substring(0, actionClassName.indexOf("Action"));

				if (StringUtils.isNotEmpty(actionPageId)) {
					if (pageId.equals(actionPageId.substring(0, 1).toLowerCase() + actionPageId.substring(1))) {
						return true;
					}
				}
			}

		}

		return flag;
	}
	
	/**
	 * 
	 * function: 反射判断要执行的方法，没有默认指定执行方法则执行doExecute方法
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * 
	 * @author Rain Stars Feb 25, 2009 2:22:06 PM
	 * @throws DBException
	 * @throws SysException
	 * @throws LogicException
	 */
	public String doAction(String doAction) throws CommonException {

		long doActionStartTime = System.currentTimeMillis();

		// 如没有指定要执行的操作，则执行doExecute方法
		if (StringUtils.isEmpty(doAction)) {

			doAction = BaseConstants.FLAG_DOEXECUTE;
		}

		logger.info(action.getClass() + " execute [" + doAction + "] start");

		// 默认跳转到执行的本页面
		String sForwardName = BaseConstants.FLAG_SUCCESS;

		// 执行操作，返回跳转路径
		sForwardName = (String) ReflectUtil.invoke(action, doAction, null, null);

		long doActionTime = System.currentTimeMillis() - doActionStartTime;

		logger.info(action.getClass() + " execute [" + doAction + "] end, cost " + doActionTime + " ms");

		return sForwardName;
	}
	
	
	/**
	 * Function: 对每个操作结束后执行的formInit方法以及对form数据的序列化<br>
	 * 
	 * Produce Describe:
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws SysException
	 * @throws DBException
	 * @throws LogicException
	 */
	protected String afterTreatment(String sForwardName) throws CommonException {

		if (action.SUCCESS.equals(sForwardName)) {

			long doActionStartTime = System.currentTimeMillis();

			logger.info(action.getClass() + " execute  [formInit ]  start");

			action.formInit();

			// /////////////////////////
			// // Get Form
			// ////////////////////////
			BaseForm form = (BaseForm) action.getModel();

			//Form2String.doForm2String(form);

			long doActionTime = System.currentTimeMillis() - doActionStartTime;

			logger.info(action.getClass() + " execute [formInit ] end, cost " + doActionTime + " ms");

		}

		return sForwardName;
	}
	
}
